<template>
	<globalModal
		:title="title"
		:visible="visible"
		:width="800"
		:bodyStyle="bodyStyle"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
	>
		<filterbar
			@handlerSearch="handlerSearch"
			@handlerReset="handlerSearch"
			:field-list="superQueryFieldList"
		></filterbar>
		<s-table
			:bordered="false"
			size="small"
			:scroll="{ y: scrollY }"
			:columns="columnslist"
			:data="getTestquestList"
			rowKey="testQuestId"
			ref="table"
			:rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'checkbox' }"
		>
			<div slot="bigNum" slot-scope="key, record">{{ record.bigNum }}-{{ record.subNum }}</div>
			<div slot="schTopicVos" slot-scope="key, record">共{{ record.schTopicVos.length }}道题</div>
		</s-table>
	</globalModal>
</template>

<script>
import { sysManageapi } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
import { clearDeep } from '@/utils/base';
export default {
	name: 'monthtestReportModal',
	mixins: [ModalMixins],
	data() {
		return {
			classList: [], //班级数组
			title: '试卷列表',
			mdl: {},
			gradeList: [],
			bodyStyle: {
				maxHeight: '450px',
				minHeight: '100px',
				overflow: 'auto',
			},
			columnslist: [
				{
					title: '序号',
					dataIndex: 'index',
					key: 'index',
					customRender: function (t, r, index) {
						return parseInt(index) + 1;
					},
					width: 80,
				},
				{
					title: '试卷名称',
					dataIndex: 'title',
					key: 'title',
				},
				{
					title: '科目',
					dataIndex: 'subjectName',
					key: 'subjectName',
				},
				{
					title: '题目',
					dataIndex: 'schTopicVos',
					key: 'schTopicVos',
					scopedSlots: {
						customRender: 'schTopicVos',
					},
					// width: 100,
				},
			],
			scrollY: 300,
			selectedRows: [], //选择行
			selectedRowKeys: [], //选择key
			superQueryFieldList: [
				{
					type: 'select',
					text: '科目',
					value: 'subjectCode',
					defaultValue: undefined,
				},
				{
					type: 'String',
					text: '试卷名称',
					value: 'title',
					defaultValue: null,
				},
			],
			filter: {},
			subjectList: [],
			testList: [],
		};
	},
	components: {},
	created() {
		this.getSubjectList();
	},
	methods: {
		show(record, testList) {
			this.mdl = { ...record };
			this.testList = [...testList];
			this.filter = {};
			this.visible = true;
			this.selectedRows = [];
			this.selectedRowKeys = [];
		},
		handlerSearch(filter) {
			this.selectedRowKeys = [];
			this.selectedRows = [];
			/* 过滤掉空对象 */
			clearDeep(filter);
			this.filter = { ...filter };
			this.$refs.table.refresh();
		},
		//获取临时试题
		getTestquestList(parameter) {
			return this.$postAction(this.$sysManageapi.testquestUrl.getList, {
				...parameter,
				orgId: this.globalOrgId,
				gradeId: this.mdl.gradeId,
				// subjectCode: this.mdl.subjectCode,
				testType: 0,
				...this.filter,
			}).then((res) => {
				return res.data;
			});
		},
		//选择试卷
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys;
			this.selectedRows = selectedRows;
		},
		getSubjectList() {
			return this.$postAction(sysManageapi.teacherUrl.getTeachSubjectList).then((res) => {
				if (res && res.code == 1) {
					this.subjectList = [];
					if (res.data && JSON.stringify(res.data) != '{}') {
						for (var key in res.data) {
							let item = {
								value: key,
								text: res.data[key],
							};
							this.subjectList.push(item);
						}
					}
					//如果是超级管理员或机构管理员或校长，添加全部
					console.log('是否校长' + JSON.stringify(this.isSchoolMaster));
					if (this.isAdmin || this.isSchoolMaster) {
						this.subjectList.unshift({
							value: '',
							text: '全部',
						});
					}
					this.superQueryFieldList[0].optionlist = [...this.subjectList];
				}
			});
		},
		handleOk() {
			if (this.selectedRows.length == 0) {
				this.$message.error('请选择一套试卷');
				return;
			}
			let obj = {};
			for (var i = 0; i < this.selectedRows.length; i++) {
				let info = this.selectedRows[i];
				if (obj[info.subjItemCode]) {
					this.$message.error('科目' + info.subjectName + '选择了多套试卷');
					return;
				}
			}
			for (var i = 0; i < this.selectedRows.length; i++) {
				let info = this.selectedRows[i];
				if (this.testList.some((item) => item.subjItemCode == info.subjItemCode)) {
					this.$message.error('已经存在科目为' + info.subjectName + '的试卷');
					return;
				}
			}
			this.$emit('ok', this.selectedRows);
			this.visible = false;
		},
	},
};
</script>

<style scoped></style>
